package org.rambadger.command.view;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.yammer.dropwizard.auth.Auth;
import com.yammer.dropwizard.views.View;
import org.mongojack.DBCursor;
import org.mongojack.DBQuery;
import org.mongojack.DBSort;
import org.mongojack.JacksonDBCollection;
import org.rambadger.command.auth.AuthUser;
import org.rambadger.command.representation.Assignment;
import org.rambadger.command.representation.Track;
import org.rambadger.command.axon.JCollection;
import org.rambadger.command.representation.Platform;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PlatformView extends View {
    @JCollection(type=Assignment.class)
    private JacksonDBCollection<Assignment, String> assignments = null;

    String title;
    boolean area_platforms = true;
    boolean hasAdmin = false;

    @JsonProperty String platformId;
    @JsonProperty String platformName;
    @JsonProperty String description;
    @JsonProperty
    ArrayList<trackListItem> trackList;
    @JsonProperty
    ArrayList<assignmentItem> freeAssignments;



    public PlatformView(@Auth(required = false) AuthUser user, Platform platform) {
        super("/restrictTrack.mustache");
        if (user!=null)
        this.hasAdmin = user.hasAdmin();
        this.platformId = platform.getPlatformId();
        this.platformName = platform.getPlatformName();
        this.description = platform.getDescription();


        this.title = ": " + platform.getPlatformName() + " Platform";
    }

    public PlatformView(@Auth(required = false) AuthUser user, Platform platform, ArrayList<Track> trackList) {
        super("/restrictTrack.mustache");
        if (user!=null)
        this.hasAdmin = user.hasAdmin();
        this.platformId = platform.getPlatformId();
        this.platformName = platform.getPlatformName();
        this.description = platform.getDescription();

        //DBCursor<Assignment> found = assignments.find(DBQuery.and(DBQuery.is("_id.platformId", platformId),DBQuery.is("associatedTrack", null)));
        //System.out.println("hey, been trying to debug you");
        //while (found.hasNext())
          //  freeAssignments.add(new assignmentItem(found.next()));

        //this.trackList = new ArrayList<trackListItem>();
        //for (int i=0;i<trackList.size();i++){
          //  this.trackList.add(new trackListItem(trackList.get(i), platformId));
        //}


        this.title = ": " + platform.getPlatformName() + " Platform";
    }

}

class trackListItem{
    @JCollection(type=Assignment.class)
    private JacksonDBCollection<Assignment, String> assignments = null;

    @JsonProperty
    String name;

    @JsonProperty
    String id;

    @JsonProperty
    ArrayList<assignmentItem> trackAssignments;

    trackListItem(Track track, String platformId){
        DBCursor<Assignment> found = assignments.find(DBQuery.and(DBQuery.is("_id.platformId", platformId),DBQuery.is("associatedTrack", this.name)));
        this.name = track.getTrackName();
        this.id = track.getTrackId().getTrackId();
        this.trackAssignments = new ArrayList<assignmentItem>();
        while (found.hasNext()){
            trackAssignments.add(new assignmentItem(found.next()));
        }

    }
}

class assignmentItem{
    @JsonProperty
    String name;

    @JsonProperty
    String id;

    public assignmentItem(Assignment assignment){
        this.name = assignment.getAssignmentName();
        this.id = assignment.getAssignmentId().getAssignmentId();
    }
}
